package light.mvc.service.admin.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.admin.Tresource;
import light.mvc.page.admin.Resource;
import light.mvc.page.base.SessionInfo;
import light.mvc.page.base.Tree;
import light.mvc.service.admin.ResourceServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ResourceServiceImpl implements ResourceServiceI {

	@Autowired
	private BaseDaoI<Tresource> resourceDao;
	
	@Override
	public List<Resource> treeGrid() {
		List<Resource> resources = new ArrayList<Resource>();
		List<Tresource> list = resourceDao
				.find("select distinct t from Tresource t left join fetch t.resource order by t.id");
		if ((list != null) && (list.size() > 0)) {
			for (Tresource t : list) {
				Resource resource = new Resource();
				BeanUtils.copyProperties(t, resource);
				if (t.getResource() != null) {
					resource.setPid(t.getResource().getId());
					resource.setPname(t.getResource().getName());
				}
				resource.setIconCls(t.getIconStr());
				resources.add(resource);
			}
		}
		return resources;
	}
	
	@Override
	public void add(Resource resource) {
		Tresource t = new Tresource();
		BeanUtils.copyProperties(resource, t);
		if ((resource.getPid() != null) ) {
			t.setResource(resourceDao.get(Tresource.class,resource.getPid()));
		}
		resourceDao.save(t);
	}

	@Override
	public void delete(Integer id) {
		Tresource t = resourceDao.get(Tresource.class, id);
		del(t);
	}

	private void del(Tresource t) {
		if ((t.getResources() != null) && (t.getResources().size() > 0)) {
			for (Tresource r : t.getResources()) {
				del(r);
			}
		}
		resourceDao.delete(t);
	}

	@Override
	public void edit(Resource resource) {
		Tresource t = resourceDao.get(Tresource.class, resource.getId());
		BeanUtils.copyProperties(resource, t);
		if ((resource.getPid() != null)) {
			t.setResource(resourceDao.get(Tresource.class, resource.getPid()));
		}
		resourceDao.update(t);
	}

	@Override
	public Resource get(Integer id) {
		Tresource t = resourceDao.get(Tresource.class, id);
		Resource resource = new Resource();
		BeanUtils.copyProperties(t, resource);
		if (t.getResource() != null) {
			resource.setPid(t.getResource().getId());
			resource.setPname(t.getResource().getName());
		}
		return resource;
	}
	
	@Override
	public List<String> resourceAllList() {
		List<String> resources = new ArrayList<String>();
		List<Tresource> l = resourceDao.find("select distinct t from Tresource t left join fetch t.resource order by t.id");
		for (int i = 0; i < l.size(); i++) {
			resources.add(l.get(i).getUrl());
		}
		return resources;
	}
	
	@Override
	public List<Tree> tree(SessionInfo sessionInfo) {
		List<Tresource> list = null;
		List<Tree> trees = new ArrayList<Tree>();

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("resourceType", 0);// 菜单类型的资源

		if (sessionInfo != null) {
			if("admin".equals(sessionInfo.getUsername())){
				list = resourceDao.find("select distinct t from Tresource t  where t.resourceType = :resourceType  order by t.id",params);
			}else{
				params.put("userId", Integer.valueOf(sessionInfo.getId()));// 自查自己有权限的资源
				list = resourceDao
					.find("select distinct t from Tresource t join t.roles role join role.users user where t.resourceType = :resourceType and user.id = :userId order by t.id",
							params);
			}
		} else {
			return null;
		}

		if ((list != null) && (list.size() > 0)) {
			for (Tresource r : list) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				tree.setText(r.getName());
				tree.setIconCls(r.getIconStr());
				Map<String, Object> attr = new HashMap<String, Object>();
				attr.put("url", r.getUrl());
				tree.setAttributes(attr);
				if (r.getResource() != null) {
					tree.setPid(r.getResource().getId().toString());
				}else{
					tree.setState("closed");
				}
				trees.add(tree);
			}
		}
		return trees;
	}
	
	@Override
	public List<Tree> allTree(boolean flag) {
		List<Tresource> list = null;
		List<Tree> trees = new ArrayList<Tree>();
		if(flag){
			list = resourceDao.find("select distinct t from Tresource t left join fetch t.resource  order by t.id");
		}else{
			list = resourceDao.find("select distinct t from Tresource t left join fetch t.resource where t.resourceType=0 order by t.id");
		}
		if ((list != null) && (list.size() > 0)) {
			for (Tresource r : list) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				tree.setText(r.getName());
				tree.setIconCls(r.getIconStr());
				Map<String, Object> attr = new HashMap<String, Object>();
				attr.put("url", r.getUrl());
				tree.setAttributes(attr);
				if (r.getResource() != null) {
					tree.setPid(r.getResource().getId().toString());
				}
				trees.add(tree);
			}
		}
		return trees;
	}

	
}
